package com.mrjy.maintain.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrjy.common.entity.SMSSendInfo;
import com.mrjy.common.entity.SalesCardInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.common.util.ResponseCode;
import com.mrjy.maintain.bo.ISalesCardInfoBo;
import com.mrjy.maintain.bo.IUserInfoBo;

@RequestMapping("/maintain/salesCard")
@Controller
public class SalesCardInfoController {

	@Resource
	private ISalesCardInfoBo salesCardInfoBo;
	@Resource
	private IUserInfoBo userInfoBo;
	
	/**
	 * 激活销售卡 --- 作废
	 * @param salesCardInfo  激活码
	 * @param smsSendInfo	手机号码，手机验证码
	 * @return
	 */
	@RequestMapping("/activateCard")
	@ResponseBody
	public Object activateCard(SalesCardInfo salesCardInfo,SMSSendInfo smsSendInfo){
		return salesCardInfoBo.activateSaleCard(salesCardInfo, smsSendInfo);
	}

    /**
     * 激活销售卡
     * @param userName 用户名称
     * @param mobilePhone 手机号码
     * @param validcode 验证码
     * @param fuCardCode 福卡激活码
     * @return
     */
    @RequestMapping("/activateFuCardPC")
    @ResponseBody
    public Object activateFuCardPC(HttpSession session, HttpServletRequest request, @RequestParam("userName")String userName, @RequestParam("mobilePhone")String mobilePhone,
                                   @RequestParam("validCode") String validcode, @RequestParam("fuCardCode") String fuCardCode, int typeId) {
        UserInfo userInfo = (UserInfo)userInfoBo.queryByMobilePhone(mobilePhone);
        String password = userInfo.getPassword().toString();
        Map<String, Object> map = salesCardInfoBo.txActivateSaleCardPC(userName, mobilePhone, password, validcode, fuCardCode, typeId);
        if (null != map && "301".equals(map.get("err_code"))) {//激活成功
            //session.invalidate();//每次登录清除Session缓存
            session = request.getSession();
            UserInfo info = (UserInfo) map.get("userInfo");
            info.setSessionId(session.getId());
            int count = userInfoBo.updateById(info);
            if (count > 0) {
                session.setAttribute(GlobalConstant.USER_INFO, info);
            }
        }
        return map;
    }

    /**
	 * 激活销售卡
	 * @param userName 用户名称
	 * @param mobilePhone 手机号码
	 * @param password 密码
	 * @param validcode 验证码
	 * @param fuCardCode 福卡激活码
	 * @return
	 */
	@RequestMapping("/activateFuCard")
	@ResponseBody
	public Object activateFuCard(HttpSession session,HttpServletRequest request, @RequestParam("userName")String userName,@RequestParam("mobilePhone")String mobilePhone,
			@RequestParam("password")String password,@RequestParam("validCode")String validcode,@RequestParam("fuCardCode")String fuCardCode){
        Map<String, Object> map =salesCardInfoBo.txActivateSaleCard(userName, mobilePhone, password, validcode , fuCardCode);
		if (null != map && "301".equals(map.get("err_code"))) {//激活成功
			//session.invalidate();//每次登录清除Session缓存
			session = request.getSession();
			UserInfo info = (UserInfo) map.get("userInfo");
			info.setSessionId(session.getId());
			int count = userInfoBo.updateById(info);
			if (count > 0) {
				session.setAttribute(GlobalConstant.USER_INFO, info);
			}
		}
		return map;
	}
	
	/**
	 * 激活销售卡 通过微信，有openid的
	 * @param userName 用户名称
	 * @param mobilePhone 手机号码
	 * @param password 密码
	 * @param validcode 验证码
	 * @param fuCardCode 福卡激活码
	 * @return
	 */
	@RequestMapping("/activateFuCardByOpenId")
	@ResponseBody
	public Object activateFuCardByOpenId(HttpSession session,HttpServletRequest request,@RequestParam("openId")String openId, @RequestParam("mobilePhone")String mobilePhone,
			@RequestParam("validCode")String validcode,@RequestParam("fuCardCode")String fuCardCode,@RequestParam("typeId")Integer typeId){
		Map<String, Object> map =salesCardInfoBo.txActivateFuCardByOpenId(openId, mobilePhone, validcode , fuCardCode,typeId);
		if (null != map && "301".equals(map.get("err_code"))) {//激活成功
			//session.invalidate();//每次登录清除Session缓存
			session = request.getSession();
			UserInfo info = (UserInfo) map.get("userInfo");
			info.setSessionId(session.getId());
			int count = userInfoBo.updateById(info);
			if (count > 0) {
				session.setAttribute(GlobalConstant.USER_INFO, info);
			}
		}
		return map;
	}
	
	/**
	 * 获取激活状态列表
	 * @return
	 */
	@RequestMapping("/getCardStatusList")
	@ResponseBody
	public List<SalesCardInfo> getCardStatusList(){
		return salesCardInfoBo.getCardStatus();
	}
	
	/**
	 * 后台获取销售卡信息
	 * @param session
	 * @param salesCardInfo
	 * @return
	 */
	@RequestMapping("/getCardListByPage")
	@ResponseBody
	public Map<String, Object> getCardListByPage(HttpSession session,SalesCardInfo salesCardInfo){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		return salesCardInfoBo.getCardByPageBack(salesCardInfo, userInfo);
	}
	
	/**
	 * 批量导出
	 * @param session
	 * @param salesCardInfo
	 * @return
	 */
	@RequestMapping("/exportCardBack")
	@ResponseBody
	public Object exportCardBack(HttpSession session,SalesCardInfo salesCardInfo,HttpServletResponse response){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		return salesCardInfoBo.exportCard(salesCardInfo, userInfo,response);
	}
	
	
	/**
	 * 批量生成销售卡
	 * @param session
	 * @param salesCardInfo
	 * @return
	 */
	@RequestMapping(value="/generateCardBatch")
	@ResponseBody
	public Object generateCardBatch(HttpSession session,SalesCardInfo salesCardInfo){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		return salesCardInfoBo.txGenerateCardBatch(salesCardInfo,userInfo);
	}
	
	
	/**
	 * 批量修改禁用状态
	 * @param session
	 * @param salesCardInfo
	 * @return
	 */
	@RequestMapping("/updateDisableFlag")
	@ResponseBody
	public Object updateDisableFlag(HttpSession session,SalesCardInfo salesCardInfo){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		return salesCardInfoBo.updateDisableFlag(salesCardInfo, userInfo);
	}
	
	
	/**
	 * 获取福卡总数,福卡总价值,总激活数,总激活金额
	 * @param salesCardInfo
	 * @return
	 */
	@RequestMapping("/getTotal")
	@ResponseBody
	public Object getTotal(SalesCardInfo salesCardInfo){
		return salesCardInfoBo.getTotal(salesCardInfo);
	}
	
	/**
	 * 导出excel
	 * 
	 * @param accessLog
	 * @return
	 */
	@RequestMapping("/export")
	@ResponseBody
	public Object export(HttpServletResponse response, SalesCardInfo salesCardInfo,HttpSession session) {
		return salesCardInfoBo.exportExl(salesCardInfo,response,session);
	}
	
	
	@RequestMapping("/generateSingleCode")
	@ResponseBody
	public Object generateSingleCode(SalesCardInfo info,HttpSession session){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		Map<String, Object> map = new HashMap<String, Object>();
		if(null == userInfo){
			map.put("err_code", ResponseCode.USER_ERROR.getCode());
			map.put("err_msg", ResponseCode.USER_ERROR.getDesc());
			return map;
		}
		info.setUserId(userInfo.getId());
		return salesCardInfoBo.generateSingleCode(info,map);
	}
	
}
